###################################################################
# Replication code for paper titled,                              #
# Strategies of Repression: Judicial and Extrajudicial Methods of #
# Autocratic Survival (2018).                                     #
# Fiona Shen-Bayh                                                 #
###################################################################

#R Version 3.3.2
rm(list=ls())
Sys.setlocale('LC_ALL','C') 

# install libraries
install.packages("plyr", "ggplot2", "reshape2", "stargazer", "extrafontdb")

# load libraries
library(plyr)
library(ggplot2)
library(reshape2)
library(stargazer)
library(extrafontdb)
font_import()     #run this command the first time you use the fonts package, alternatively try "loadfonts()"

# set working directory to wherever file is stored
set_wd("~/")
# load csv file
data <- read.csv("sb_judicial_repression_wp_2018.csv", stringsAsFactors=F) 

# set factor levels
data$challenger_type <- as.factor(data$challenger_type)
data$challenger_type <- factor(data$challenger_type, levels =c("Insiders", "Outsiders"))

data$actor_type <- as.factor(data$actor_type)
data$actor_type <- factor(data$actor_type, levels = c("Ruling Party", "Military","Opposition"))

data$opp_type <- as.factor(data$opp_type)
data$opp_type <- factor(data$opp_type, levels = c("Ruling Party", "Military", "Non-Defector", "Defector"))

#################
# Main Analysis #
#################

##### Logit Analysis, repression on challenger-type (Table 1)
r1 <- glm(trial ~ challenger_type, data=data, na.action=na.omit, family = binomial(link="logit"))
r2 <- glm(trial ~ challenger_type + exec_mil, data=data, na.action=na.omit, family = binomial(link="logit"))
r3 <- glm(trial ~ challenger_type + exec_mil + I(country) + as.factor(year), data, family = binomial(link="logit"))

names(r1$coefficients)[1:2] <- c('Insider','Outsider') #rename variables
names(r2$coefficients)[1:2] <- c('Insider','Outsider') #rename variables
names(r3$coefficients)[1:2] <- c('Insider','Outsider') #rename variables

##### Logit analysis, repression on actor-type (Table 2)
r4 <- glm(trial ~ actor_type, data=data, na.action=na.omit, family = binomial(link="logit"))
r5 <- glm(trial ~ actor_type + exec_mil, data=data, na.action=na.omit, family = binomial(link="logit"))
r6 <- glm(trial ~ actor_type + exec_mil + I(country) + as.factor(year), data, family = binomial(link="logit"))

names(r4$coefficients)[1:3] <- c('Ruling Party','Military', 'Opposition') #rename variables
names(r5$coefficients)[1:3] <- c('Ruling Party','Military', 'Opposition') #rename variables
names(r6$coefficients)[1:3] <- c('Ruling Party','Military', 'Opposition') #rename variables

###### Logit analysis, repression on opposition-type (Table 3)
r7 <- glm(trial ~ opp_type, data=data, na.action=na.omit, family = binomial(link="logit"))
r8 <- glm(trial ~ opp_type + exec_mil, data=data, na.action=na.omit, family = binomial(link="logit"))
r9 <- glm(trial ~ opp_type + exec_mil + I(country) + as.factor(year), data, family = binomial(link="logit"))

names(r7$coefficients)[1:4] <- c('Ruling Party','Military', 'Non-Defector', 'Defector') #rename variables
names(r8$coefficients)[1:4] <- c('Ruling Party','Military', 'Non-Defector', 'Defector') #rename variables
names(r9$coefficients)[1:4] <- c('Ruling Party','Military', 'Non-Defector', 'Defector') #rename variables

######################
# Tables and Figures #
######################

# Figure 1: Repression Tactics, Independence to 1994
# Figure 2: Repression Strategy by Challenger-Type, Independence to 1994 
# Figure 3: Predicted Probability of Going to Trial, Independence to 1994: Insiders and Outsiders 
# Figure 4: Predicted Probability of Going to Trial, Independence to 1994: Party, Military, Opposition
# Figure 5: Predicted Probability of Going to Trial, Independence to 1994: Outsider Defectors and Non-Defectors
# Figure 6: Long-Run Outcomes, Independence to 1994 


# Table 1: Logit Regression of Repression on Challenger-Type: Insiders and Outsiders 
# Table 2: Logit Regression of Repression on Challenger-Type: Ruling Party, Military, and Opposition
# Table 3: Logit Regression of Repression on Challenger-Type: Outsider Non-Defectors and Defectors


# Font Formatting for plots
times_font <- theme(plot.title = element_text(family = "Times New Roman", face="bold", size=10, hjust=0.5),
                    axis.title = element_text(family = "Times New Roman", size=10),
                    axis.title.x = element_text(family="Times New Roman", margin = unit(c(5, 0, 0, 0), "mm")),
                    axis.title.y = element_text(family="Times New Roman", margin = unit(c(0, 5, 0, 0), "mm")),
                    plot.caption = element_text(family = "Times New Roman", size=10),
                    text=element_text(family="Times New Roman"))

# Figure 1
# Repression Tactics, Independence to 1994
rep_tactics <- ddply(data, .(challenger_type), summarize,
                     Trial = sum(trial, na.rm=T),
                     Imprison = sum(prison, na.rm=T),
                     Deport = sum(deport, na.rm=T),
                     Execute = sum(execute, na.rm=T))
rep_tactics <- melt(rep_tactics)

ggplot(rep_tactics, aes(x=variable, y=value))+
  geom_bar(stat="identity", position="dodge", width=0.5)+
  scale_fill_grey(name="Challenger Type", start = 0.2, end = 0.5, na.value = "red")+
  xlab("")+ylab("Total Number of Challengers")+
  theme_classic()+times_font

# Figure 2
# Repression Strategy by Challenger-Type, Independence to 1994 
bivariate <- ddply(data, .(challenger_type), summarize,
                   Judicial = sum(trial),
                   Extrajudicial = sum(trial==0))

bivariate <- melt(bivariate, id.vars="challenger_type")

ggplot(bivariate, aes(x=challenger_type, y=value, fill=as.factor(variable)))+
  geom_bar(stat="identity", position="dodge", width=0.5)+
  scale_fill_grey(name="Repression Strategy", start = 0.2, end = 0.6, na.value = "red", labels=c("Judicial", "Extrajudicial"))+
  xlab("")+ylab("Total Number of Challengers")+
  theme_classic()+times_font

# Figure 3
# Predicted Probability of Going to Trial, Independence to 1994: Insiders and Outsiders 

# Function to calculate and plot predicted probabilities
pr.prob.plot <- function(regression, groups){
  names(regression$coefficients)[1:2] <- groups
  model.matrix <- data.frame(challenger_type = groups)
  model.matrix <- cbind(model.matrix, predict(regression, newdata=model.matrix, type="response", se.fit=TRUE))
  names(model.matrix)[names(model.matrix)=="fit"] = "prob"
  names(model.matrix)[names(model.matrix)=="se.fit"] = "se.prob"
  model.matrix$ll = model.matrix$prob - 1.96*model.matrix$se.prob
  model.matrix$ul = model.matrix$prob + 1.96*model.matrix$se.prob
  
  plot <- ggplot(model.matrix, aes(x=challenger_type, y = prob)) +
    geom_bar(stat="identity", width=0.3, fill="gray")+
    geom_errorbar(aes(ymin = ll, ymax = ul), width = 0.1, lty=1, lwd=1, col="black") +
    scale_x_discrete(limits = groups)+
    ylim(0, 1)+
    labs(title="", x="", y="Predicted Probability of Going to Trial", caption = "") +
    theme_classic()+times_font
  return(plot)
}

pr.prob.plot(r1, groups=c("Insiders", "Outsiders"))

# Figure 4
# Predicted Probability of Going to Trial, Independence to 1994: Party, Military, Opposition

# Function to calculate and plot predicted probabilities
pr.prob.plot.2 <- function(regression, groups){
  names(regression$coefficients)[1:3] <- groups
  model.matrix <- data.frame(actor_type = groups)
  model.matrix <- cbind(model.matrix, predict(regression, newdata=model.matrix, type="response", se.fit=TRUE))
  names(model.matrix)[names(model.matrix)=="fit"] = "prob"
  names(model.matrix)[names(model.matrix)=="se.fit"] = "se.prob"
  model.matrix$ll = model.matrix$prob - 1.96*model.matrix$se.prob
  model.matrix$ul = model.matrix$prob + 1.96*model.matrix$se.prob
  
  plot <- ggplot(model.matrix, aes(x=actor_type, y = prob)) +
    geom_bar(stat="identity", width=0.5, fill="gray")+
    geom_errorbar(aes(ymin = ll, ymax = ul), width = 0.2, lty=1, lwd=1, col="black") +
    scale_x_discrete(limits = groups)+
    ylim(0, 1)+
    labs(title="", x="", y="Predicted Probability of Going to Trial", caption = "") +
    theme_classic()+times_font
  return(plot)
}

pr.prob.plot.2(r4, groups=c("Ruling Party", "Military", "Opposition"))

# Figure 5
# Predicted Probability of Going to Trial, Independence to 1994: Outsider Defectors and Non-Defectors

# Function to calculate and plot predicted probabilities
pr.prob.plot.3 <- function(regression, groups){
  names(regression$coefficients)[1:4] <- groups
  model.matrix <- data.frame(opp_type = groups)
  model.matrix <- cbind(model.matrix, predict(regression, newdata=model.matrix, type="response", se.fit=TRUE))
  names(model.matrix)[names(model.matrix)=="fit"] = "prob"
  names(model.matrix)[names(model.matrix)=="se.fit"] = "se.prob"
  model.matrix$ll = model.matrix$prob - 1.96*model.matrix$se.prob
  model.matrix$ul = model.matrix$prob + 1.96*model.matrix$se.prob
  
  plot <- ggplot(model.matrix, aes(x=opp_type, y = prob)) +
    geom_bar(stat="identity", width=0.3, fill="gray")+
    geom_errorbar(aes(ymin = ll, ymax = ul), width = 0.1, lty=1, lwd=1, col="black") +
    scale_x_discrete(limits = groups)+
    ylim(0, 1)+
    labs(title="", x="", y="Predicted Probability of Going to Trial", caption = "") +
    theme_classic()+times_font
  return(plot)
}

pr.prob.plot.3(r7, groups=c("Ruling Party", "Military", "Non-Defector", "Defector"))


# Figure 6
# Long-Run Outcomes, Independence to 1994 
rep_outcomes <- ddply(data, .(trial), summarize,
                      prison = sum(prison, na.rm=T),
                      pardon = sum(pres_commute, na.rm=T))
rep_outcomes <- melt(rep_outcomes, id.vars="trial")


ggplot(rep_outcomes, aes(x=variable, y=value, fill=as.factor(trial)))+
  geom_bar(stat="identity", position="dodge", width=0.4)+
  scale_fill_grey(name="Repression Strategy",
                  start = 0.2, end = 0.6, na.value = "red",
                  labels=c("No Trial", "Trial"))+
  xlab("")+ylab("Total Number of Challengers")+
  scale_x_discrete(labels=c("Imprison", "Commute/Pardon"))+
  theme_classic()+times_font

# Table 1
# Logit Regression of Repression on Challenger-Type: Insiders and Outsiders 
stargazer(r1, r2, r3, 
          omit=c("country", "year"),
          omit.labels=c("Country FE", "Year FE"),
          omit.stat=c("AIC"))


# Table 2
# Logit Regression of Repression on Challenger-Type: Ruling Party, Military, and Opposition
stargazer(r4, r5, r6, 
          omit=c("country", "year"),
          omit.labels=c("Country FE", "Year FE"),
          omit.stat=c("AIC"))


# Table 3
# Logit Regression of Repression on Challenger-Type: Outsider Non-Defectors and Defectors
stargazer(r7, r8, r9, 
          omit=c("country", "year"),
          omit.labels=c("Country FE", "Year FE"),
          omit.stat=c("AIC"))
